home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / objtools / totri.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  116 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* 
  18.  *    totri -
  19.  *        Convert an object to triangles.
  20.  *
  21.  *            Paul Haeberli - 1990
  22.  */
  23. #include "math.h"
  24. #include "stdio.h"
  25. #include "vect.h"
  26. #include "sgiobj.h"
  27.  
  28. sgiobj *sgototri();
  29.  
  30. main(argc,argv)
  31. int argc;
  32. char **argv;
  33. {
  34.     sgiobj *obj;
  35.  
  36.     if(argc<3) {
  37.     fprintf(stderr,"usage: totri in.sgo out.sgo [-randord]\n");
  38.     exit(1);
  39.     }
  40.     obj = readsgiobj(argv[1]);
  41.     if(argc>=3)
  42.     obj = sgototri(obj,1);
  43.     else
  44.     obj = sgototri(obj,0);
  45.     writesgiobj(argv[2],obj);
  46.     exit(0);
  47. }
  48.  
  49. int count;
  50.  
  51. countfunc()
  52. {
  53.     count++;
  54. }
  55.  
  56. float *fptr;
  57. int *ordtab;
  58.  
  59. copytri(p0,p1,p2)
  60. float *p0, *p1, *p2;
  61. {
  62.     float *dptr; 
  63.  
  64.     dptr = fptr + ordtab[count]*(3*PNTLONGS);
  65.     bcopy(p0,dptr,PNTLONGS*sizeof(long));
  66.     dptr+= PNTLONGS;
  67.     bcopy(p1,dptr,PNTLONGS*sizeof(long));
  68.     dptr+= PNTLONGS;
  69.     bcopy(p2,dptr,PNTLONGS*sizeof(long));
  70.     count++;
  71. }
  72.  
  73. randorder(tab,len)
  74. int tab[];
  75. {
  76.     int i, pos, temp;
  77.  
  78.     if(len<2)
  79.     return;
  80.     srandom(getpid());
  81.     for(i=0; i<len; i++) {
  82.     pos = i+random()%(len-i);
  83.     temp = tab[pos];
  84.     tab[pos] = tab[i];
  85.     tab[i] = temp;
  86.     }
  87. }
  88.     
  89. sgiobj *sgototri(obj,randord)
  90. sgiobj *obj;
  91. int randord;
  92. {
  93.     sgiobj *tobj;
  94.     int i;
  95.  
  96.     if((randord == 0) && obj->objtype == OBJ_TRILIST)
  97.     return clonesgiobj(obj);
  98.  
  99.     count = 0;
  100.     applytotris(obj,countfunc);
  101.  
  102.     ordtab = (int *)malloc(count*sizeof(int));
  103.     for(i=0; i<count; i++)
  104.     ordtab[i] = i;
  105.     if(randord) 
  106.     randorder(ordtab,count);
  107.  
  108.     tobj = (sgiobj *)newtriobj(count);
  109.     fptr = (float *)tobj->data;
  110.     count = 0;
  111.     applytotris(obj,copytri);
  112.  
  113.     free(ordtab);
  114.     return tobj;
  115. }
  116.